#include "app_device.h"

// 创建结构体存放缓冲区句柄,是否运行标志位
typedef struct
{

    bool is_runing;

    dobbuffer_handle_t UploadBuffer;
    dobbuffer_handle_t DownloadBuffer;

} app_device_t;

app_device_t my_app_device = {.is_runing = true};

void mqtt_recv_cb(void *data, int len);

// 声明上传线程函数
static void Upload_task(void *arg);
// 声明下载线程函数
static void Download_task(void *arg);

/// @brief 初始化
/// @param
/// @return
Com_state_t app_device_init(void)
{
    // 初始化Modbus
    if (app_Modbus_init() == Com_FAIL)
    {
        log_error("Modbus初始化失败");
        return Com_FAIL;
    }
    log_info("Modbus初始化成功");


    // 开启mqtt
    if (app_mqtt_init() == Com_FAIL)
    {
        log_error("mqtt初始化失败");
        return Com_FAIL;
    }
    log_info("mqtt初始化成功");
    app_mqtt_recv_callback_register(mqtt_recv_cb);
    // 初始化线程池

    if (app_pool_init(4) == Com_FAIL)
    {
        log_error("线程池初始化失败");
        return Com_FAIL;
    }
    log_info("线程池初始化成功");

    // 创建上传线程 X2
    app_pool_add_task(Upload_task, NULL);
    app_pool_add_task(Upload_task, NULL);
    // 创建下载线程 X2
    app_pool_add_task(Download_task, NULL);
    app_pool_add_task(Download_task, NULL);

    // 初始化缓冲区 创建两个 一个上传一个下载
    my_app_device.UploadBuffer = app_dobbuffer_init(1024);
    my_app_device.DownloadBuffer = app_dobbuffer_init(1024);

    // 置位启动标志
    log_info("置位启动标志 true");
    app_dev_set_is_running(true);
    return Com_OK;
}

/// @brief 删除任务
/// @param
/// @return
Com_state_t app_device_deinit(void)
{
    app_mqtt_deinit();
    app_pool_deinit();
    app_dobbuffer_deinit(my_app_device.UploadBuffer);
    app_dobbuffer_deinit(my_app_device.DownloadBuffer);
    app_dev_set_is_running(false);
    exit(0);
    return Com_OK;
}

/// @brief 判断程序是否正常运行
/// @param
/// @return 返回com标志
Com_state_t app_device_is_runing(void)
{
    if (my_app_device.is_runing)
    {
        return Com_OK;
    }
    else
    {
        return Com_FAIL;
    }
}

/// @brief 设置程序是否启动
/// @param is_runing 
void app_dev_set_is_running(bool is_runing)
{
    my_app_device.is_runing = is_runing;
}

/// @brief 上传任务
/// @param arg
static void Upload_task(void *arg)
{
    char data[255] = {0};
    while (1)
    {
        int realLen = app_dobbuffer_read(my_app_device.UploadBuffer, data, sizeof(data));
        if (realLen > 0)
        {
            log_debug("realLen:%d",realLen);
            data[realLen] = '\0';
            app_mqtt_send((void*)data, realLen);
        }
    }
}

/// @brief 下载任务 将mqtt的json数据进行解析赋值给结构体
/// @param arg
static void Download_task(void *arg)
{
    char data[255] = {0};
    app_json_t Download_json = {0};
    while (1)
    {
        // 循环读取缓冲区数据
        int realLen = app_dobbuffer_read(my_app_device.DownloadBuffer, data, sizeof(data));
        // 如果收到数据
        if (realLen > 0)
        {
            log_debug("data:%s", data);
            // 将数据进行解析
            if (app_json_unpack(data, &Download_json) == Com_OK)
            {

                // 打印结果
                log_debug("action: %s, connetType: %s, motorId: %d, motorSpeed: %d, status: %s",
                          Download_json.action,
                          Download_json.connetType,
                          Download_json.motorId,
                          Download_json.motorSpeed,
                          Download_json.status);

                // 对解析的值进行判断 发消息给modbus
                if (strcmp(Download_json.action, "set") == 0)
                {
                    // 给modubs发送数据, 不需要接收
                    app_Modbus_write_single_holde_register(Download_json.motorId,(uint16_t)Download_json.motorSpeed);

                }
                else if (strcmp(Download_json.action, "get") == 0)
                {
                    // 给modubs发送数据, 需要接收
                    // 解析完成后清空缓冲区
                    log_debug("清空data 1");
                    memset(data, 0, strlen(data));

                    Com_state_t st = app_Modbus_read_single_input_register(Download_json.motorId,(uint16_t *)Download_json.motorSpeed);
                    if (st = Com_OK)
                    {
                        strcpy(Download_json.status,"Ok");
                    }
                    else
                    {
                        strcpy(Download_json.status,"Fail");
                    }
                    
                    app_json_depack(&Download_json, data);
                    // 发走
                    log_debug("// 发走 datalen: %d,data:%s",strlen(data),data);
                    app_dobbuffer_write(my_app_device.UploadBuffer, data, strlen(data));
                }
                // 解析完成后清空缓冲区
                log_debug("清空data 2");
                memset(data, 0, sizeof(data));
            }
        }
    }
}

void mqtt_recv_cb(void *data, int len)
{
    app_dobbuffer_write(my_app_device.DownloadBuffer, (char *)data, (uint8_t)len);
}
